/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package albumdownloader.domain.service;

import albumdownloader.domain.entity.Album;
import albumdownloader.domain.entity.Page;
import albumdownloader.domain.entity.Song;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author max.m.yuan@gmail.com
 */
public class YiTingMusicServiceImpl implements MusicService {
    private static final String ALBUM_URL = "http://so.1ting.com/album.do";
    private static final int PAGE_SIZE = 20;
    
    @Override
    public Page<Album> search(String q, int pageSize, int pageIndex) throws IOException {
        int maxPages = 0;
        List<Album> albums = new ArrayList<>();
        int start = pageIndex * pageSize;
        int pageStart = ((int) (start / PAGE_SIZE)) + 1;
        int _start = start % PAGE_SIZE;
        boolean isFirstPage = true;
        for(int i = pageStart; albums.size() < pageSize; i++) {
            Document doc = Jsoup.connect(ALBUM_URL)
                    .data("q", q).data("page",new Integer(i).toString()).get();
            Elements lis = doc.select("#resultList li");
            if(lis == null || lis.isEmpty()) {
                break;
            }
            int j = 0;
            if(isFirstPage) {
                j = _start;
                String maxRows = doc.select("div.pTitle strong").get(1).text();
                int _maxRows = Integer.parseInt(maxRows);
                maxPages = _maxRows > 0 ? ((int) (_maxRows / pageSize)) + 1 : 0;
                isFirstPage = false;
            }
            for (; j < lis.size() && albums.size() < pageSize; j ++) {
                Element li = lis.get(j);
                String name = li.select("span.albumName").get(0).text();
                String artist = li.select("span.singerName").get(0).text();
                String coverUrl = li.select("img.albumPic").get(0).attr("src");
                String bigCoverUrl = coverUrl.replace("s100", "s300");
                String url = li.select("a.albumLink").get(0).attr("href");
                albums.add(new Album(name, artist, coverUrl, bigCoverUrl, url));
            }
            if(lis.size() < PAGE_SIZE) {
                break;
            }
        }
        return (new Page<>(maxPages, pageIndex, albums));
    }

    @Override
    public List<Song> listSongs(Album album) throws IOException {
        Document doc = Jsoup.connect(album.getUrl()).get();
        List<Song> songs = new ArrayList<>();
        Elements trs = doc.select("#song-list tr");
        for(Element tr : trs) {
            String id = tr.select("td.songId").get(0).text();
            Element a = tr.select("a.songName").get(0);
            String name = a.text();
            String url = a.attr("href");
            songs.add(new Song(id, name, url));
        }
        return songs;
    }
}
